<?php

/**
 * commentaire actions.
 *
 * @package    explications_informatiques
 * @subpackage search
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchActions extends sfActions
{
  private function NoResSearch($req)
  {
	  $res = new Noressearch();
	  $res->setSearch($req);
	  $res->save();
  }
  public function executeArticle(sfWebRequest $request)
  {
     $this->forwardUnless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT),"headers","forbidden");
	 $badswords = array("un","une","des","le","la","les", "à", "a","donc","mais","ou","où","or","ni","car","avec","sans","version");
	 
	 if(isset($request["words"]))
	 {
        $request["words"] = str_replace(array(",","?",".",";",":","/","!","%","&","\"","#","'","(",")","[","]","{","}"),"",$request["words"]);
		$sql_where = "";
		$sql_where2 = "";
		$words = explode(" ",$request["words"]);
		$i = 0;
		foreach($words as $word)
			if(!in_array($word,$badswords))
			{
				$word = sfCoreAutoload::sluggify($word);
				if($i == 0)
				{
					$sql_where .= "ao.word = '".strtolower(utf8_decode($word))."' ";
					$sql_where2 .= "a.title LIKE '%".utf8_decode($word)."%' ";
				}
				else
				{
					$sql_where .= "OR ao.word = '".utf8_decode(strtolower($word))."'";
					$sql_where2 .= "OR a.title LIKE '%".utf8_decode($word)."%' ";
				}
				$i++;
			}
			
		$q = Doctrine_Query::create()
	 	->from('articles a')
		->leftJoin('a.Assoplatform ap')
		->leftJoin('ap.Correctionsortho ao')
		->where($sql_where)
		->orWhere($sql_where2);
		
		$this->Results = $q->execute();
		$this->nb = $q->count();
		if($q->count() == 0)
			$this->NoResSearch($request["words"]);
	 }
  }

  public function executeLexique(sfWebRequest $request)
  {
     $sql_where = "";
	 $sql_where2 = "";
	 $words = explode(" ",$request["words"]);
	 $i = 0;
	
		foreach($words as $word)
		{
			
			if($i == 0)
				{
					$sql_where .= "lm.mot = '".strtolower($word)."' ";
					$sql_where2 .= "ld.definition LIKE '%".$word."%' ";
				}
				else
				{
					$sql_where .= "OR lm.mot = '".strtolower($word)."'";
					$sql_where2 .= "OR ld.definition LIKE '%".$word."%' ";
				}
				$i++;
		}
			
		$q = Doctrine_Query::create()
	 	->from('lexiquemot lm')
		->leftJoin('lm.Lexiquedefinition ld')
		->orderBy('ld.vote desc')
		->where($sql_where)
		->orWhere($sql_where2);
		
		$this->Results = $q->execute();
		$this->nb = $q->count();
		if($q->count() == 0)
			$this->NoResSearch($request["words"]);
  }

  public function executeCommun(sfWebRequest $request)
  {
   		$res1 =  $this->executeArticle($request);
		$res2 =  $this->executeLexique($request);
		if(!is_array($res1))
			$res1 = array();
			
		if(!is_array($res2))
			$res2 = array();
			
		$this->Results = array_merge($res1,$res2);
		
  }

  public function executePlatform(sfWebRequest $request)
  {
  	 $q = Doctrine_Query::create()
	 	->from('assoplatform ap')
		->leftJoin('ap.Articles')
		->leftJoin('ap.Plateformversiongroup')
		->leftJoin('ap.Plateformversiongroup_5')
		->leftJoin('ap.Platform')
		->leftJoin('ap.Platform_4')
		->where('id_major = ?',$request->getParameter("id"))
		->orWhere('id_minor = ?',$request->getParameter("id"));
		
	$this->Results = $q->execute();
  }

  public function executeFavoris(sfWebRequest $request)
  {
	 
	if($this->getUser()->isAuthenticated())
	{ 
		$q = Doctrine_Query::create()
			->from('favoris f')
			->leftJoin('f.Assoplatform ap')
			->leftJoin('f.Articles')
			->leftJoin('ap.Plateformversiongroup')
			->leftJoin('ap.Plateformversiongroup_5')
			->leftJoin('ap.Platform')
			->leftJoin('ap.Platform_4')
			->where('f.id_client = ?',$this->getUser()->getGuardUser()->getId());
	}
	else
	 	$q = array();
	$this->nb = $q->count();
	$this->Results = $q->execute();
  }

  public function executeTag(sfWebRequest $request)
  {
    $q = Doctrine_Query::create()
	 	->from('tags t')
		->leftJoin('t.Articles')
		->where('t.tag LIKE "%'.str_replace(array("%20",'\'', "\""),"_",$request->getParameter("word")).'%"');
	$this->Results = $q->execute();
	$this->nb = $q->count();
  }

}
